package com.cardreader;

import java.io.File;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;

import com.bluetooth.lib.BluetoothUtil;
import com.bluetooth.lib.client.BluetoothClient;
import com.bluetooth.lib.client.DispatchMsg;
import com.bluetooth.lib.client.BluetoothClient.ConnectCallBack;
import com.bluetooth.lib.client.BluetoothClient.SearchCallBack;
import com.bluetooth.lib.client.BluetoothClient.SendUTFCallBack;
import com.bluetooth.lib.impl.ATRCallBack;
import com.bluetooth.lib.impl.CardReaderImp;
import com.bluetooth.lib.impl.SendCallback;
import com.bluetooth.lib.util.HexUtil;

@SuppressLint("DefaultLocale")
public class CardReaderApi implements CardReaderImp {
	BluetoothClient client;
	private SearchCallBack searchCallBack;
	private DispatchMsg mdispatchMsg;
	public CardReaderApi(Context context) {
		client = BluetoothUtil.createBluetoothClient(context);
	}

	@Override
	public String getCardReaderVersion() throws CardReaderException {
		// TODO Auto-generated method stub
		DispatchMsg msg = null;
		try {
			msg = sendCmd("FEFEF800");
		} catch (CardReaderException e) {
			throw new CardReaderException("读卡版本错误", e.getExceptionCode());
		}
		return msg.data;
	}

	@Override
	public String restCardReader() throws CardReaderException {
		// TODO Auto-generated method stub
		DispatchMsg msg=null;
		try {
			msg = sendCmd("FEFEF802");
		} catch (CardReaderException e) {
			throw new CardReaderException("读卡器复位错误", e.getExceptionCode());
		}
		return msg.data;
	}

	@Override
	public String trasmit(String hexData) throws CardReaderException {
		// TODO Auto-generated method stub
		byte[] byte_leght = new byte[2];
		int lenght = hexData.length() / 2;
		byte_leght[1] = (byte) (lenght & 0xff);
		byte_leght[0] = (byte) (lenght >> 8 & 0xff);
		String cmdHex = "FEFEF803" + HexUtil.bytesToHex(byte_leght) + hexData;
		DispatchMsg msg = null;
		try {
			msg=sendCmd(cmdHex);
			//Log.d("JonPage", msg.cmd+""HexUtil.IntToByteOneHex(mInt))
		} catch (CardReaderException e) {
			// TODO: handle exception
			throw new CardReaderException("上发数据错误", e.getExceptionCode());
		}
		return msg.data;
	}

	@Override
	public void setOnATRListener(ATRCallBack callBack) {
		// TODO Auto-generated method stub
		client.setOnATRCallBack(callBack);

	}
	private DispatchMsg sendCmd(String cmdHex) throws CardReaderException {
		DispatchMsg msg = client.sendDatabyWait(HexUtil.hexToBytes(cmdHex));
		if (msg.cmd.toUpperCase().equals("FEFEF805")) {
			throw new CardReaderException("错误", msg.data);
		}
		return msg;
	}

	@Override
	public BluetoothClient getBluetoothClient() {
		// TODO Auto-generated method stub
		return client;
	}

	@Override
	public void search(SearchCallBack call) {
		// TODO Auto-generated method stub
		this.searchCallBack=call;
		client.stopSearch();
		client.search(call);
	}

	@Override
	public void connect(BluetoothDevice device, ConnectCallBack call) {
		// TODO Auto-generated method stub
		if (client.isBonded(device)) {
			client.connect(device, call);
		} else {
			try {
				autoBond(device.getClass(), device, "1234");
				client.createBond(device);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				Log.d("JonPage", e.getMessage() + "配对失败");
				e.printStackTrace();
			}

		}

	}

	@Override
	public void sendUTF(String msg, SendUTFCallBack call) {
		// TODO Auto-generated method stub
		client.sendUTF(msg, call);

	}

	@Override
	public void sendData(byte[] data) {
		// TODO Auto-generated method stub
		client.sendData(data);
	}

	@Override
	public void sendFile(File file, boolean isbreak) {
		// TODO Auto-generated method stub
		client.sendFile(file, isbreak);

	}

	@Override
	public void close() {
		// TODO Auto-generated method stub
		client.close();
	}

	private boolean autoBond(Class btClass, BluetoothDevice device,
			String strPin) throws Exception {
		Method autoBondMethod = btClass.getMethod("setPin",
				new Class[] { byte[].class });
		Boolean result = (Boolean) autoBondMethod.invoke(device,
				new Object[] { strPin.getBytes() });
		return result;
	}

	@Override
	public boolean getConnectState() {
		// TODO Auto-generated method stub
		if (client!=null) {
			
			return client.getConnectState();
		}
		return false;
	}

	@Override
	public void stopSearch() {
		// TODO Auto-generated method stub
		client.stopSearch();
	}

	@Override
	public void restSearch() {
		// TODO Auto-generated method stub
		search(searchCallBack);
	}

}
